// pages/dragTest/dragTest.js
var startPoint;
Page({
  data: {
    buttonTop: 100,
    buttonLeft: 0,
    windowHeight: '',
    windowWidth: '',
    moveFlag: false,
  },

  onLoad: function (options) {
    var self = this;
    wx.getSystemInfo({
      success: function (res) {
        console.log(res);
        // 屏幕宽度、高度
        console.log('height=' + res.windowHeight, 'width=' + res.windowWidth);
        // 高度,宽度 单位为px
        self.setData({
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth,
          buttonLeft: res.windowWidth - 70,
        })
      }
    });
  },

  onShow: function () {

  },

  buttonStart: function (e) {
    startPoint = e.touches[0]
  },
  buttonMove: function (e) {
    // console.log('buttonMove', e);
    var endPoint = e.touches[e.touches.length - 1]
    var translateX = endPoint.clientX - startPoint.clientX
    var translateY = endPoint.clientY - startPoint.clientY
    startPoint = endPoint
    var buttonTop = this.data.buttonTop + translateY
    var buttonLeft = this.data.buttonLeft + translateX
    //判断是移动否超出屏幕
    if (buttonLeft + 70 >= this.data.windowWidth) {
      buttonLeft = this.data.windowWidth - 70;
    }
    if (buttonLeft <= 0) {
      buttonLeft = 0;
    }
    if (buttonTop <= 0) {
      buttonTop = 0
    }
    if (buttonTop + 50 >= this.data.windowHeight) {
      buttonTop = this.data.windowHeight - 50;
    }
    this.setData({
      buttonTop: buttonTop,
      buttonLeft: buttonLeft
    });
    //移动标记
    var moveFlag = this.data.moveFlag;
    // console.log('translateX=', translateX, 'translateY=', translateY)
    //防抖操作
    if (Math.abs(translateX) > 0.5 || Math.abs(translateX) > 0.5) {
      moveFlag = true;
    }
    this.setData({
      moveFlag: moveFlag,
    });
  },
  buttonEnd: function (e) {
    console.log('buttonEnd', e);
    var moveFlag = this.data.moveFlag;
    if (moveFlag) {
      //移动结束后恢复
      moveFlag = false;
      this.setData({
        moveFlag: moveFlag,
      });
    } else {
      console.log('没有移动，点击按钮', e);
      this.categoryScan();
    }
  },

  categoryScan() {
    console.log('categoryScan');
    wx.showToast({
      title: '点击按钮了',
      icon: 'none'
    })
  },

})